package aidturith.ihm.action.key;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import aidturith.Constant;
import aidturith.ihm.panel.proto.TablePanel;

public class AlphabetAction extends AbstractAction {

	private static final long serialVersionUID = 1L;

	private TablePanel panel;

	private char a;
	private int row = -1;
	private int count = 0;

	// TODO long?
	public AlphabetAction(TablePanel panel, char a) {
		this.panel = panel;
		this.a = a;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(!panel.isEditing()) {
			if(Constant.DEBUG) System.out.println("push "+a);
			Object obj = null;
			while(true) {
				do {
					if(row >= (panel.getVisibleRowCount()-1)) row = -1;
					row++;
					count++;
					obj = panel.getModel().getValueAt(panel.getTable().convertRowIndexToModel(row), 0);
					if(obj.toString().toUpperCase().charAt(0) == a) {
						if(Constant.DEBUG) System.out.println("found "+obj);
						panel.selectRow(row);
						count = 0;
						return;
					}
					if(count == (panel.getTable().getRowCount()-1)) {
						count = 0;
						return;
					}
				} while(row < (panel.getTable().getRowCount()-1));
				row = -1;
			}
		}
	}

}
